package com.hyh.music.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hyh.music.Result.PageResult;
import com.hyh.music.Result.Results;
import com.hyh.music.domain.domainDTO.SearchDTO;
import com.hyh.music.domain.domainDTO.SongDTO;
import com.hyh.music.domain.domainDTO.SongPageDTO;
import com.hyh.music.domain.domainDTO.SongUpdateDTO;
import com.hyh.music.domain.domains.Singer;
import com.hyh.music.domain.domains.Song;
import com.hyh.music.domain.domianVO.SearchVO;
import com.hyh.music.domain.domianVO.SongVO;
import com.hyh.music.mapper.SingerMapper;
import com.hyh.music.mapper.SongMapper;
import com.hyh.music.service.SongService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hyh
 * @version 1.0
 */
@Slf4j
@Service
@SuppressWarnings({"all"})
public class SongServiceImpl implements SongService {


    @Autowired
    SongMapper songMapper;
    @Autowired
    SingerMapper singerMapper;
    /**
     * 歌曲的分页查询
     * @param songPageDTO
     * @return
     */
    @Override
    public PageResult PageSelect(SongPageDTO songPageDTO) {
        PageHelper.startPage(songPageDTO.getPage(),songPageDTO.getPageSize());
        Page<Song> songs = songMapper.page(songPageDTO);
        long total = songs.getTotal();
        List<Song> result = songs.getResult();
        PageResult pageResult = new PageResult(total, result);
        return pageResult;
    }
    /**
     * 添加歌曲
     * @param songDTO
     */
    @Override
    public void addSong(SongDTO songDTO) {
        Song song = new Song();
        BeanUtils.copyProperties(songDTO,song);
        song.setUpdateTime(LocalDateTime.now());
        song.setCreateTime(LocalDateTime.now());
        songMapper.addSong(song);
    }

    /**
     * 根据id查询歌曲
     * @param id
     * @return
     */
    @Override
    public Song selectById(Integer id) {
       Song song = songMapper.selectById(id);
       return song;
    }

    /**
     * 更新歌曲
     * @param songUpdateDTO
     */
    @Override
    public void update(SongUpdateDTO songUpdateDTO) {
        Song song = new Song();
        BeanUtils.copyProperties(songUpdateDTO,song);
        song.setUpdateTime(LocalDateTime.now());
        songMapper.update(song);
    }

    /**
     * 删除歌曲
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        songMapper.deleteById(id);
    }

    /**
     * 搜索所有歌曲
     * @return
     */
    @Override
    public Results selectAllSongs() {
       List<Song> songs = songMapper.selectAllSongs();
        List<SongVO> list = new ArrayList<>();
        for (Song song : songs) {
            Integer singerId = song.getSingerId();
            Singer singer = singerMapper.selectById(singerId);
            String name = singer.getName() + "-" + song.getName();
            SongVO songVO = new SongVO(song.getId(),name);
            list.add(songVO);
        }
       return new Results(list);
    }

    /**
     * 默认搜索
     * @param searchDTO
     */
    @Override
    public PageResult page(SearchDTO searchDTO) {
        PageHelper.startPage(searchDTO.getPage(),searchDTO.getPageSize());
        Page<SearchVO> songs = songMapper.select(searchDTO);
        long total = songs.getTotal();
        List<SearchVO> result = songs.getResult();
        PageResult pageResult = new PageResult(total, result);
        return pageResult;
    }
}
